package com.st.mall.address.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.st.mall.address.model.AddressModel;
import com.st.mall.address.service.IAddressService;

@Controller
@RequestMapping("/address")
public class AddressController {
	
	@Autowired
	private IAddressService service;
	
	@RequestMapping("/insert")
	@ResponseBody
	public String insert(AddressModel addrModel) {
		String result = service.insert(addrModel);
		return result;
	}
	
	@RequestMapping(value = "/selectModel",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectModel(AddressModel addrModel) {
		Map<String,Object> map = new HashMap<>();
		List<AddressModel> list = service.selectModel(addrModel);
		int result = service.selectCount(addrModel);
		map.put("data",list);
		map.put("count",result);
		return new JSONObject(map).toString();
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int delete(AddressModel addrModel) {
		System.out.println(addrModel);
		return service.delete(addrModel);	
	}
	
	@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String get(AddressModel addrModel) {
		AddressModel result = service.selectId(addrModel);
		System.out.println(result);
		if(result==null) {
			return "1";
		}
		return new JSONObject(result).toString();
	}
	
	@RequestMapping("/updAddr")
	@ResponseBody
	public int update(AddressModel addrModel) {
		int result = service.update(addrModel);
		return result;
	}
	
	
}
